package com.minisoft.workhour.service;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.minisoft.workhour.model.ManhourRequest;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.List;

/**
 * JSON文件读取服务
 */
public class JsonFileService {

    /**
     * 从JSON文件读取工时数据
     *
     * @param filePath JSON文件路径
     * @return 工时项列表
     */
    public List<ManhourRequest.ManhourItem> readManhourDataFromFile(String filePath) {
        if (filePath == null || filePath.trim().isEmpty()) {
            throw new IllegalArgumentException("文件路径不能为空");
        }

        try {
            Path path = Paths.get(filePath);
            if (!Files.exists(path)) {
                throw new IllegalArgumentException("文件不存在: " + filePath);
            }

            System.out.println("开始读取JSON文件: " + filePath);

            // 读取文件内容
            String content = new String(Files.readAllBytes(path), "UTF-8");
            if (content == null || content.trim().isEmpty()) {
                throw new IllegalArgumentException("文件内容为空");
            }

            System.out.println("文件内容: " + content);

            // 解析JSON数组
            List<ManhourRequest.ManhourItem> manhourItems = JSON.parseObject(
                content,
                new TypeReference<List<ManhourRequest.ManhourItem>>() {}
            );

            if (manhourItems == null || manhourItems.isEmpty()) {
                throw new IllegalArgumentException("JSON文件中没有有效的工时数据");
            }

            System.out.println("成功读取到 " + manhourItems.size() + " 条工时记录");

            // 验证数据完整性
            validateManhourItems(manhourItems);

            return manhourItems;

        } catch (IOException e) {
            System.err.println("读取文件失败: " + filePath + ", 错误: " + e.getMessage());
            throw new RuntimeException("读取文件失败: " + e.getMessage(), e);
        } catch (Exception e) {
            System.err.println("解析JSON文件失败: " + filePath + ", 错误: " + e.getMessage());
            throw new RuntimeException("解析JSON文件失败: " + e.getMessage(), e);
        }
    }

    /**
     * 验证工时数据的完整性
     *
     * @param manhourItems 工时项列表
     */
    private void validateManhourItems(List<ManhourRequest.ManhourItem> manhourItems) {
        for (int i = 0; i < manhourItems.size(); i++) {
            ManhourRequest.ManhourItem item = manhourItems.get(i);

            if (item.getMode() == null || item.getMode().trim().isEmpty()) {
                throw new IllegalArgumentException(String.format("第%d条记录的mode不能为空", i + 1));
            }
            if (item.getOwner() == null || item.getOwner().trim().isEmpty()) {
                throw new IllegalArgumentException(String.format("第%d条记录的owner不能为空", i + 1));
            }
            if (item.getTask() == null || item.getTask().trim().isEmpty()) {
                throw new IllegalArgumentException(String.format("第%d条记录的task不能为空", i + 1));
            }
            if (item.getType() == null || item.getType().trim().isEmpty()) {
                throw new IllegalArgumentException(String.format("第%d条记录的type不能为空", i + 1));
            }
            if (item.getStart_time() <= 0) {
                throw new IllegalArgumentException(String.format("第%d条记录的start_time必须大于0", i + 1));
            }
            if (item.getHours() <= 0) {
                throw new IllegalArgumentException(String.format("第%d条记录的hours必须大于0", i + 1));
            }
            if (item.getDescription() == null || item.getDescription().trim().isEmpty()) {
                throw new IllegalArgumentException(String.format("第%d条记录的description不能为空", i + 1));
            }

            System.out.println("验证通过 - 记录" + (i + 1) + ": task=" + item.getTask() +
                ", hours=" + item.getHours() + ", description=" + item.getDescription());
        }

        System.out.println("所有工时记录验证通过");
    }
}
